﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TicketApp.Web.TGIServiceProxy;
using System.Configuration;
using System.Data;

namespace TicketApp.Web.Controllers
{
    public class OrganizerController : Controller
    {
        //
        // GET: /Organizer/

        public ActionResult Index()
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var organizers = context.AllUsers.Where(user => user is Organizer);
            return View(organizers);
        }

        //
        // GET: /Organizer/Details/5

        public ActionResult Details(int id)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var organizer = (Organizer)context.AllUsers.Expand("Address").Where(user => user is Organizer && user.Id == id).FirstOrDefault();

            return View(organizer);
        }

        //
        // GET: /Organizer/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Organizer/Create

        [HttpPost]
        public ActionResult Create(Organizer organizer)
        {

            if (ModelState.IsValid)
            {
                var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
                
                //context.MergeOption = System.Data.Services.Client.MergeOption.AppendOnly;
                var role = context.Roles.Where(r => r.Name.ToLower() == "organizer").FirstOrDefault();
               // var address = new Address() { Line1 = "Line1", Line2 = "Line2", PostalCode = "1111", State = "State", City = "City", Country = "Country" };
                var address = organizer.Address;
                context.AddToAddresses(address);
                

               // user.Address = address;
                organizer.Role = role;
                
                context.AddToAllUsers(organizer);
                
                context.SetLink (organizer, "Role", role); 
                context.SetLink(organizer, "Address", address); 

                context.SaveChanges();
                
            }

            return RedirectToAction("Index");
        }

        //
        // GET: /Organizer/Edit/5

        public ActionResult Edit(int id)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var organizer = (Organizer)context.AllUsers.Expand("Address").Where(user => user is Organizer && user.Id == id).FirstOrDefault();
            return View(organizer);
        }

        //
        // POST: /Organizer/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Organizer organizer)
        {

            if (ModelState.IsValid)
            {
                var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
                context.AttachTo("AllUsers", organizer);
                context.AttachTo("Addresses", organizer.Address);
                context.UpdateObject(organizer.Address);
                context.UpdateObject(organizer);
                
                context.SaveChanges( System.Data.Services.Client.SaveChangesOptions.Batch);

            }

            return RedirectToAction("Index");
        }

        //
        // GET: /Organizer/Delete/5

        public ActionResult Delete(int id)
        {
            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            var organizer = (Organizer)context.AllUsers.Expand("Address").Where(user => user is Organizer && user.Id == id).FirstOrDefault();
            return View(organizer);
        }

        //
        // POST: /Organizer/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Organizer organizerToBeDeleted)
        {

            var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var organizer = (Organizer)context.AllUsers.Expand("Address").Where(user => user is Organizer && user.Id == id).FirstOrDefault();
            context.AttachTo("AllUsers", organizerToBeDeleted);
            context.AttachTo("Addresses", organizerToBeDeleted.Address);
            context.DeleteObject(organizerToBeDeleted);
            context.DeleteObject(organizerToBeDeleted.Address);
            context.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch);
            return RedirectToAction("Index");

           
        }
    }
}
